package com.ffk.wiki.controller;


import com.ffk.wiki.domain.Ebook;
import com.ffk.wiki.req.EbookQueryReq;
import com.ffk.wiki.req.EbookSaveReq;
import com.ffk.wiki.resp.CommonResp;
import com.ffk.wiki.resp.EbookResp;
import com.ffk.wiki.resp.PageResp;
import com.ffk.wiki.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Resource
    private EbookService ebookService;
    /**
     * 查询所有电子书的方法
     */
    @GetMapping("/getAllEbook")
    public CommonResp<List<Ebook>> selectAllEbook(){
        List<Ebook> list = ebookService.getAll();
        CommonResp<List<Ebook>> resp = new CommonResp<List<Ebook>>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return  resp;
    }

    /**
     * 带查询条件的查询（因为不能确定具体多少个参数，所以封装请求参数）
     * 封装请求参数的模拟方法
     */
    @GetMapping("/getEbook")
    public CommonResp<PageResp<EbookResp>> selectEbook(@Valid EbookQueryReq ebookReq){
        PageResp<EbookResp> list = ebookService.getEbook(ebookReq);
        CommonResp<PageResp<EbookResp>> resp = new CommonResp<>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return  resp;
    }

    /**
     * 保存方法
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        return resp;
    }

    /**
     * 删除方法

     */
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
}
